<?php
class CostpointsController extends Controller{

	public function index(){
		$this->getSmarty();
		$this->smarty->display("admin/costpoints.tpl");
	}


	public function saveOrModify() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$service = new CommonDataService( $this->getDB () );

		$id = $_POST ["id"];
		if (empty ( $id )) {
			$input_data = array (
					'id' =>  uniqid (),
					'costpoints' => $_POST ["costpoints"],
					'card' => $_POST ["card"],
					'unit' => "$",
					'img' =>  $_POST ["img"],
					'desc' =>  $_POST ["desc"],
					'create_time' => date('Y-m-d'),
			);
			$data = $service->add("cost_points_card",$input_data );
		} else {

			$input_data = array (
					'costpoints' => $_POST ["costpoints"],
					'card' => $_POST ["card"],
					'unit' => "$",
					'img' =>  $_POST ["img"],
					'desc' =>  $_POST ["desc"]
			);

			$input_condition = array (
					'id' => $_POST ["id"]
			);
			$data = $service->edit("cost_points_card",$input_data, $input_condition );
		}
		echo "1";
	}
	function deleteSubject() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$service = new CommonDataService( $this->getDB () );
		$id =" id = '".trim ($_POST ['id'])."'";
		$service->delete("cost_points_card",$id);
		echo -1;
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );

		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		$result = array ();
		$row_count = $service->countNum ("cost_points_card");
		$result ["total"] = $row_count;
		$resource_infos = $service->resourcePage ("cost_points_card" ,$offset, $rows);
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
	function check() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );
		$id = $_POST ['id'] ;
		$result=$service->getResult("select * from user_points_approval_card where id='".$id."'");
		$type = $_POST ['type'] ;
		if(!empty($result)){
			if($type == 0){
				//审核通过
				$service->exSql("update user_points_approval_card set checked = 1,state = 1 ,approvalid ='".$_SESSION['aduser']->id."',approvalname ='".$_SESSION['aduser']->user_name."' ,checked_time ='".date("Y-m-d")."' where id ='".$id."'" );
				//$service->exSql("update users set points = points-".$result->costpoints."  where userid ='".$result->userid."'" );
				//$service->
			}
			if($type == 1){
				//审核不通过
				$service->exSql("update users set points = points+".$result->costpoints."  where userid ='".$result->userid."'" );
				$service->exSql("update user_points_approval_card set checked = 1,state = 0 ,approvalid ='".$_SESSION['aduser']->id."',approvalname ='".$_SESSION['aduser']->user_name."' ,checked_time ='".date("Y-m-d")."' where id ='".$id."'" );
			
				$invitePointsRecord = array (
					'id' => uniqid (),
					'userid' => $result->userid,
					'username' => $result->username,
					'record_type' => 0,
					'points' => $result->costpoints,
					'points_type' => 3,
					'points_desc'=>"Approval card fail return back ".$result->costpoints,
					'create_time' =>date("Y-m-d h:m:s")
				);
				$service->add("user_points_record",$invitePointsRecord);
			}
		}
	}

	function getApprovalpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );

		$USERNAME = trim ( $_POST ['USERNAME'] );
		$APPROVALNAME = trim ( $_POST ['APPROVALNAME'] );
		$CREATE_TIME = trim ( $_POST ['CREATE_TIME'] );
		$CHECKED = trim ( $_POST ['CHECKED'] );

		if(!empty($CREATE_TIME)){
			$CREATE_TIME = date('Y-m-d ', strtotime($CREATE_TIME));
		}
		$STATUS = trim ( $_POST ['STATUS'] );
		$condition = $this->getCondition ( $USERNAME, $APPROVALNAME, $CREATE_TIME, $STATUS,$CHECKED );
		//echo ($condition);
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		$result = array ();
		$row_count = $service->countNum ("user_points_approval_card",$condition);
		$result ["total"] = $row_count;
		$resource_infos = $service->resourcePage ("user_points_approval_card" ,$offset, $rows,$condition);
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}


	function getCondition($USERNAME, $APPROVALNAME, $CREATE_TIME, $STATUS,$CHECKED) {
		$condition = array ();
		if (! empty ( $USERNAME )) {
			$condition ['username'] = "username like '%" . $USERNAME . "%'";
		}
		if (! empty ( $APPROVALNAME )) {
			$condition ['approvalname'] = "approvalname like '%" . $APPROVALNAME . "%'";
		}
		if (! empty ( $CREATE_TIME )) {
			$condition ['create_time'] = "create_time ='" . $CREATE_TIME . "'";
		}
		if (! empty ( $STATUS )) {
			$condition ['state'] = "state = " . $STATUS ;
		}
		if (! empty ( $CHECKED )) {
			$condition ['checked'] = "checked = " . $CHECKED ;
		}
		$i = 0;
		foreach ( $condition as $key => $value ) {
				
			if ($i == count ( $condition ) - 1) {
				$conditions .= $value;
			} else {
				$conditions .= $value . " and ";
			}
			$i ++;
		}
		return $conditions;
	}
}